package com.java110.mall.cart.bmo.cart.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.mall.cart.bmo.cart.ICartBMO;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.factory.Java110TransactionalFactory;
import com.java110.dto.app.AppDto;
import com.java110.dto.coupon.CouponShopPoolDto;
import com.java110.dto.groupBuy.GroupBuyProductSpecDto;
import com.java110.dto.product.ProductCategoryDto;
import com.java110.dto.product.ProductDto;
import com.java110.dto.product.ProductSpecValueDto;
import com.java110.dto.vipUser.ShopVipAccountDto;
import com.java110.dto.vipUser.ShopVipLevelDto;
import com.java110.dto.vipUser.ShopVipUserDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.dto.storeOrder.StoreOrderCartEventDto;
import com.java110.dto.storeOrder.StoreOrderCartFeeDto;
import com.java110.dto.storeOrder.StoreOrderDiscountDto;
import com.java110.dto.user.UserAddressDto;
import com.java110.intf.acct.*;
import com.java110.intf.cart.*;
import com.java110.intf.dev.IAppV1InnerServiceSMO;
import com.java110.intf.goods.IGroupBuyProductSpecInnerServiceSMO;
import com.java110.intf.goods.IProductInnerServiceSMO;
import com.java110.intf.goods.IProductSpecValueInnerServiceSMO;
import com.java110.intf.shop.ICouponConfInnerServiceSMO;
import com.java110.intf.shop.IShopVipLevelInnerServiceSMO;
import com.java110.intf.shop.IShopVipUserInnerServiceSMO;
import com.java110.intf.user.IUserAddressInnerServiceSMO;
import com.java110.po.coupon.CouponShopPoolPo;
import com.java110.po.coupon.CouponShopPoolDetailPo;
import com.java110.po.coupon.CouponUserPo;
import com.java110.po.groupBuy.GroupBuyProductSpecPo;
import com.java110.po.product.ProductSpecValuePo;
import com.java110.po.shop.ShopVipAccountPo;
import com.java110.po.shop.ShopVipAccountDetailPo;
import com.java110.po.shop.ShopVipUserPo;
import com.java110.po.storeOrder.StoreOrderPo;
import com.java110.po.storeOrder.StoreOrderAddressPo;
import com.java110.po.storeOrder.StoreOrderCartPo;
import com.java110.po.storeOrder.StoreOrderCartEventPo;
import com.java110.po.storeOrder.StoreOrderCartFeePo;
import com.java110.po.storeOrder.StoreOrderDiscountPo;
import com.java110.utils.lock.DistributedLock;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service("cartBMOImpl")
public class CartBMOImpl implements ICartBMO {

    private final static Logger logger = LoggerFactory.getLogger(CartBMOImpl.class);

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartInnerServiceSMO storeOrderCartInnerServiceSMOImpl;

    @Autowired
    private IProductInnerServiceSMO productInnerServiceSMOImpl;

    @Autowired
    private IProductSpecValueInnerServiceSMO productSpecValueInnerServiceSMOImpl;

    @Autowired
    private IGroupBuyProductSpecInnerServiceSMO groupBuyProductSpecInnerServiceSMOImpl;

    @Autowired
    private IUserAddressInnerServiceSMO userAddressInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderAddressInnerServiceSMO storeOrderAddressInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartEventInnerServiceSMO storeOrderCartEventInnerServiceSMOImpl;

    @Autowired
    private IAppV1InnerServiceSMO appInnerServiceSMOImpl;
    @Autowired
    private IShopVipUserInnerServiceSMO shopVipUserInnerServiceSMOImpl;
    @Autowired
    private IShopVipLevelInnerServiceSMO shopVipLevelInnerServiceSMOImpl;
    @Autowired
    private IShopVipAccountInnerServiceSMO shopVipAccountInnerServiceSMOImpl;
    @Autowired
    private IShopVipAccountDetailInnerServiceSMO shopVipAccountDetailInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartFeeInnerServiceSMO storeOrderCartFeeInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderDiscountInnerServiceSMO storeOrderDiscountInnerServiceSMOImpl;
    @Autowired
    private ICouponConfInnerServiceSMO couponConfInnerServiceSMOImpl;
    @Autowired
    private ICouponUserV1InnerServiceSMO couponUserV1InnerServiceSMOImpl;
    @Autowired
    private ICouponShopPoolDetailV1InnerServiceSMO couponShopPoolDetailV1InnerServiceSMOImpl;
    @Autowired
    private ICouponShopPoolV1InnerServiceSMO couponShopPoolV1InnerServiceSMOImpl;

    /**
     * @param param {
     *              "storeOrder":{
     *              "personId":"",
     *              "personName":"",
     *              "remark":"",
     *              "extOrderId":"",
     *              "payPrice":""
     *              },
     *              "storeOrderCarts":[
     *              {
     *              "productId":"",
     *              "prodName":"",
     *              "valueId":"",
     *              "goodsNum":"",
     *              "storeId":"",
     *              "shopId":"",
     *              }
     *              ],
     *              "addressId":""
     *              <p>
     *              }
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> unifiedOrder(JSONObject param) {

        logger.debug("统一下单入参：", param.toJSONString());

        if (!param.containsKey("storeOrder")) {
            throw new IllegalArgumentException("不包含订单信息");
        }

        if (!param.containsKey("storeOrderCarts")) {
            throw new IllegalArgumentException("不包含订单项");
        }

        JSONObject storeOrder = param.getJSONObject("storeOrder");
        JSONArray storeOrderCarts = param.getJSONArray("storeOrderCarts");

        if (storeOrderCarts == null || storeOrderCarts.size() < 1) {
            throw new IllegalArgumentException("不包含订单项");
        }

        AppDto appDto = new AppDto();
        appDto.setAppId(param.getString("appId"));
        List<AppDto> appDtos = appInnerServiceSMOImpl.queryApps(appDto);

        Assert.listOnlyOne(appDtos, "应用不存在");

        StoreOrderPo storeOrderPo = BeanConvertUtil.covertBean(storeOrder, StoreOrderPo.class);
        storeOrderPo.setOrderId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_orderId));
        storeOrderPo.setPayPrice(storeOrder.getString("payPrice"));
        storeOrderPo.setAppId(appDtos.get(0).getAppId());
        storeOrderPo.setAppName(appDtos.get(0).getName());
        storeOrderPo.setTotalPrice("0");
        storeOrderPo.setFreightPrice("0");
        storeOrderPo.setState(StoreOrderDto.STATE_WAIT_PAY);
        if (AppDto.MALL_APP_ID.equals(appDtos.get(0).getAppId())) {
            storeOrderPo.setState(StoreOrderDto.STATE_FINISH_PAY);
        }
        storeOrderPo.setOId(StringUtil.isEmpty(Java110TransactionalFactory.getOId()) ? "-1" : Java110TransactionalFactory.getOId());
        if (!storeOrder.containsKey("personId") || StringUtil.isEmpty(storeOrder.getString("personId"))) {
            storeOrderPo.setPersonId("-1");
        }

        if (!storeOrder.containsKey("personName") || StringUtil.isEmpty(storeOrder.getString("personName"))) {
            storeOrderPo.setPersonName("未知");
        }

        JSONObject goods = null;
        String shopId = null;
        String storeId = null;
        for (int goodsIndex = 0; goodsIndex < storeOrderCarts.size(); goodsIndex++) {
            goods = storeOrderCarts.getJSONObject(goodsIndex);
            if (null == shopId || null == storeId || "".equals(shopId) || "".equals(storeId)) {
                shopId = goods.getString("shopId");
                storeId = goods.getString("shopId");
            }
            saveStoreOrderCart(goods, storeOrderPo, param);
        }
        int flag = storeOrderInnerServiceSMOImpl.saveStoreOrder(storeOrderPo);

        if (flag < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
        }

        //如果商品又优惠写入优惠
        if (storeOrder.containsKey("discountPrice") && !StringUtil.isEmpty(storeOrder.getString("discountPrice"))) {
            dealOrderDiscountPrice(storeOrder, storeOrderPo, storeOrderCarts);
        }

        //保存收货人信息
        if (param.containsKey("addressId")) {
            saveOrderAddress(storeOrderPo, param.getString("addressId"));
        }

        //计算积分和金币
        JSONObject vipuser = param.getJSONObject("vipUsers");
        if (null != vipuser && vipuser.size() > 0) {

            if (!StringUtil.isNullOrNone(vipuser.getString("vipId"))) {
                saveOrderIntegral(vipuser, shopId, storeOrder, storeId, storeOrderPo);
                StoreOrderCartDto storeOrderCartDto = new StoreOrderCartDto();
                storeOrderCartDto.setOrderId(storeOrderPo.getOrderId());
                storeOrderCartDto.setVipId(vipuser.getString("vipId"));
                storeOrderCartDto.setShopId(shopId);
                storeOrderCartDto.setPayPrice(storeOrder.getString("payPrice"));
                storeOrderInnerServiceSMOImpl.saveOrderGoldCount(storeOrderCartDto);
            }
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "成功", storeOrderPo);
        //统一下单流程
        // 保存订单
        // select * from store_order t;


        // 保存下单商品
        //select * from store_order_cart t;

        //保存联系人
        //select * from store_order_address t;
        // 商品库存扣减

//
//        return ResultVo.createResponseEntity(param);
    }

    private void dealOrderDiscountPrice(JSONObject storeOrder, StoreOrderPo storeOrderPo, JSONArray storeOrderCarts) {
        double discountPrice = storeOrder.getDouble("discountPrice");
        if (discountPrice == 0) { // 没有优惠
            return;
        }

        JSONObject goods = storeOrderCarts.getJSONObject(0);

        StoreOrderDiscountPo storeOrderDiscountPo = new StoreOrderDiscountPo();
        storeOrderDiscountPo.setDiscountId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_discountId));
        storeOrderDiscountPo.setDiscountObjId(storeOrderPo.getOrderId());
        storeOrderDiscountPo.setDiscountObjType(StoreOrderDiscountDto.DISCOUNT_OBJ_TYPE_ORDER);
        storeOrderDiscountPo.setDiscountPrice(discountPrice + "");
        storeOrderDiscountPo.setDiscountRate(storeOrder.getString("discountRate"));
        storeOrderDiscountPo.setDiscountType(storeOrder.getString("discountType"));
        storeOrderDiscountPo.setShopId(goods.getString("shopId"));
        int flag = storeOrderDiscountInnerServiceSMOImpl.saveStoreOrderDiscount(storeOrderDiscountPo);
        if (flag < 1) {
            throw new IllegalArgumentException("记录优惠信息失败");
        }
    }

    private void saveOrderIntegral(JSONObject vipuser, String shopId, JSONObject storeOrder, String storeId, StoreOrderPo storeOrderPo) {

        double amount = Double.parseDouble(storeOrder.getString("payPrice"));
        //查询会员是否存在
        ShopVipUserDto shopVipUserDto = new ShopVipUserDto();
        shopVipUserDto.setShopId(shopId);
        shopVipUserDto.setVipId(vipuser.getString("vipId"));
        List<ShopVipUserDto> shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        Assert.listOnlyOne(shopVipUserDtos, "会员不存在");

        ShopVipLevelDto shopVipLevelDto = new ShopVipLevelDto();
        shopVipLevelDto.setShopId(shopId);
        shopVipLevelDto.setVipLevelId(shopVipUserDtos.get(0).getVipLevel());
        List<ShopVipLevelDto> shopVipLevelDtos = shopVipLevelInnerServiceSMOImpl.queryShopVipLevels(shopVipLevelDto);
        Assert.listOnlyOne(shopVipLevelDtos, "会员等级不存在");

        double goldCostMoney = Double.parseDouble(shopVipLevelDtos.get(0).getGoldCostMoney());

        double goldCount = Math.floor(amount / goldCostMoney);
        //向下取整
        double integralCount = Math.floor(amount / Double.parseDouble(shopVipLevelDtos.get(0).getIntegralCostMoney()));
        ShopVipAccountDto shopVipAccountDto = new ShopVipAccountDto();
        shopVipAccountDto.setVipId(vipuser.getString("vipId"));
        shopVipAccountDto.setAcctType("2004");
        List<ShopVipAccountDto> shopVipAccountDtos = shopVipAccountInnerServiceSMOImpl.queryShopVipAccounts(shopVipAccountDto);
        if (shopVipAccountDtos == null || shopVipAccountDtos.size() < 1) {
            ShopVipAccountPo shopVipAccountPo = new ShopVipAccountPo();
            shopVipAccountPo.setVipAcctId(GenerateCodeFactory.getGeneratorId("11"));
            shopVipAccountPo.setAcctName("积分账户");
            shopVipAccountPo.setAmount("0");
            shopVipAccountPo.setShopId(shopId);
            shopVipAccountPo.setStoreId(storeId);
            shopVipAccountPo.setVipId(vipuser.getString("vipId"));
            shopVipAccountPo.setAcctType("2004");
            int flag = shopVipAccountInnerServiceSMOImpl.saveShopVipAccount(shopVipAccountPo);
            if (flag < 1) {
                throw new IllegalArgumentException("新增积分账户失败");
            }
            ShopVipAccountDto vipAccountDto = new ShopVipAccountDto();
            vipAccountDto.setVipAcctId(shopVipAccountPo.getVipAcctId());
            vipAccountDto.setStoreId(shopVipAccountPo.getStoreId());
            vipAccountDto.setShopId(shopVipAccountPo.getShopId());
            vipAccountDto.setAmount(shopVipAccountPo.getAmount());
            vipAccountDto.setAcctType(shopVipAccountPo.getAcctType());
            shopVipAccountDtos.add(vipAccountDto);

        }
        ShopVipAccountDetailPo shopVipAccountDetailPo = new ShopVipAccountDetailPo();
        shopVipAccountDetailPo.setVipAcctId(shopVipAccountDtos.get(0).getVipAcctId());
        shopVipAccountDetailPo.setShopId(shopVipAccountDtos.get(0).getShopId());
        shopVipAccountDetailPo.setOrderId(storeOrderPo.getOrderId());
        shopVipAccountDetailPo.setStoreId(shopVipAccountDtos.get(0).getStoreId());
        shopVipAccountDetailPo.setAmount(integralCount + "");
        shopVipAccountDetailPo.setRemark("收银台累计积分");
        int detaFlag = shopVipAccountInnerServiceSMOImpl.prestoreAccount(shopVipAccountDetailPo);
        if (detaFlag < 1) {
            throw new IllegalArgumentException("更新积分账户明细失败");
        }
    }


    /**
     * 手工赠送优惠卷
     *
     * @param json
     */
    @Java110Transactional
    public ResponseEntity<String> saveSendGoldCount(JSONObject json) {


        //赠送优惠卷数量
        BigDecimal goldCostMoney = new BigDecimal(json.getString("goldCostMoney"));
        //优惠卷池ID
        String spId = json.getString("spId");
        //商铺ID
        String shopId = json.getString("shopId");
        //会员ID
        String vipId = json.getString("vipId");


        //查询会员是否存在
        ShopVipUserDto shopVipUserDto = new ShopVipUserDto();
        shopVipUserDto.setShopId(shopId);
        shopVipUserDto.setVipId(vipId);
        List<ShopVipUserDto> shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        Assert.listOnlyOne(shopVipUserDtos, "会员不存在");

        ShopVipLevelDto shopVipLevelDto = new ShopVipLevelDto();
        shopVipLevelDto.setShopId(json.getString("shopId"));
        shopVipLevelDto.setVipLevelId(shopVipUserDtos.get(0).getVipLevel());
        List<ShopVipLevelDto> shopVipLevelDtos = shopVipLevelInnerServiceSMOImpl.queryShopVipLevels(shopVipLevelDto);
        Assert.listOnlyOne(shopVipLevelDtos, "会员等级不存在");

        int runNumber = 0;
        int tmpStock = 0;

        //查询店铺所拥有的购物券，验证库存与扣除购物券关系
        CouponShopPoolDto couponShopPoolDto = new CouponShopPoolDto();
        couponShopPoolDto.setShopId(shopId);
        couponShopPoolDto.setSpId(spId);
        List<CouponShopPoolDto> couponShopPoolDtos = couponShopPoolV1InnerServiceSMOImpl.queryCouponShopPools(couponShopPoolDto);
        BigDecimal stock = new BigDecimal(couponShopPoolDtos.get(0).getStock());
        //库存为0的情况下直接返回不做购物券处理
        if (0 == stock.intValue()) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "赠送失败，购物券库存不足");
        }
        //校验库存与所需扣除数量，相等或者库存不够，库存有多少送多少，
        if ((stock.subtract(goldCostMoney).setScale(0, BigDecimal.ROUND_DOWN).doubleValue()) <= 0) {
            runNumber = stock.intValue();
        } else {
            runNumber = goldCostMoney.intValue();
        }
        //根据数量 按照一个购物券为一条数据，为用户写入购物券明细
        for (int userIndex = 0; userIndex < runNumber; userIndex++) {
            CouponUserPo couponUserPo = new CouponUserPo();
            couponUserPo.setCouponId(GenerateCodeFactory.getGeneratorId("11"));
            couponUserPo.setCouponName(couponShopPoolDtos.get(0).getCouponName());
            couponUserPo.setActualPrice(couponShopPoolDtos.get(0).getActualPrice());
            couponUserPo.setPoolId(couponShopPoolDtos.get(0).getPoolId());
            couponUserPo.setValidityDay(couponShopPoolDtos.get(0).getValidityDay());
            couponUserPo.setUserId(shopVipUserDtos.get(0).getVipId());
            couponUserPo.setUserName(shopVipUserDtos.get(0).getVipName());
            couponUserPo.setTel(shopVipUserDtos.get(0).getVipTel());
            couponUserPo.setState(CouponUserPo.STATE_RUN);
            int detaFlag = couponUserV1InnerServiceSMOImpl.saveCouponUser(couponUserPo);
            if (detaFlag < 1) {
                throw new IllegalArgumentException("购物券写入用户信息表失败");
            }
        }
        if ((stock.subtract(goldCostMoney).setScale(0, BigDecimal.ROUND_DOWN).doubleValue()) <= 0) {
            tmpStock = 0;
        } else {
            tmpStock = stock.subtract(goldCostMoney).setScale(0, BigDecimal.ROUND_DOWN).intValue();
        }
        //根据赠送数量刷新店铺购物券库存数据
        CouponShopPoolPo couponShopPoolPo = new CouponShopPoolPo();
        couponShopPoolPo.setSpId(couponShopPoolDtos.get(0).getSpId());
        couponShopPoolPo.setStock(tmpStock + "");
        int detaFlag = couponShopPoolV1InnerServiceSMOImpl.updateCouponShopPool(couponShopPoolPo);
        if (detaFlag < 1) {
            throw new IllegalArgumentException("店铺购物券刷新库存失败");
        }
        //记录赠送明细表
        CouponShopPoolDetailPo couponShopPoolDetailPo = new CouponShopPoolDetailPo();
        couponShopPoolDetailPo.setDetailId(GenerateCodeFactory.getGeneratorId("11"));
        couponShopPoolDetailPo.setCouponName(couponShopPoolDtos.get(0).getCouponName());
        couponShopPoolDetailPo.setPoolId(couponShopPoolDtos.get(0).getPoolId());
        couponShopPoolDetailPo.setShopId(couponShopPoolDtos.get(0).getShopId());
        couponShopPoolDetailPo.setSpId(couponShopPoolDtos.get(0).getSpId());
        couponShopPoolDetailPo.setActualPrice(couponShopPoolDtos.get(0).getActualPrice());
        couponShopPoolDetailPo.setSendCount(runNumber + "");
        couponShopPoolDetailPo.setValidityDay(couponShopPoolDtos.get(0).getValidityDay());
        couponShopPoolDetailPo.setOrderId("-1");
        couponShopPoolDetailPo.setUserId(shopVipUserDtos.get(0).getVipId());
        couponShopPoolDetailPo.setUserName(shopVipUserDtos.get(0).getVipName());
        couponShopPoolDetailPo.setTel(shopVipUserDtos.get(0).getVipTel());
        detaFlag = couponShopPoolDetailV1InnerServiceSMOImpl.saveCouponShopPoolDetail(couponShopPoolDetailPo);
        if (detaFlag < 1) {
            throw new IllegalArgumentException("记录店铺购物券赠送明细失败");
        }
        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "赠送成功！");
    }

    /**
     * 手工赠送停车券
     *
     * @param json
     */
    @Java110Transactional
    public ResponseEntity<String> saveSendParking(JSONObject json, String storeId) {
        //车牌号
        String licensePlate = json.getString("licensePlate");
        //优惠卷池ID
        String spId = json.getString("spId");
        //优惠卷个数
        BigDecimal giftsCostMoney = new BigDecimal(json.getString("giftsCostMoney"));
        //商铺ID
        String shopId = json.getString("shopId");
        //会手机号
        String tel = json.getString("tel");

        ShopVipLevelDto shopVipLevelDto = new ShopVipLevelDto();
        shopVipLevelDto.setShopId(json.getString("shopId"));
        //查询会员最低等级 三级信息
        shopVipLevelDto.setVipLevel(ShopVipLevelDto.VIP_LEVEL_THREE);
        List<ShopVipLevelDto> shopVipLevelDtos = shopVipLevelInnerServiceSMOImpl.queryShopVipLevels(shopVipLevelDto);
        if (shopVipLevelDtos == null || shopVipLevelDtos.size() < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "赠送失败，请先在会员等级功能中维护一个三级会员！");
        }

        //查询会员是否存在
        ShopVipUserDto shopVipUserDto = new ShopVipUserDto();
        shopVipUserDto.setShopId(shopId);
        shopVipUserDto.setVipName(licensePlate);
        shopVipUserDto.setVipTel(tel);
        List<ShopVipUserDto> shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        ShopVipUserPo shopVipUserPo = new ShopVipUserPo();
        if (shopVipUserDtos == null || shopVipUserDtos.size() < 1) {

            shopVipUserPo.setShopId(shopId);
            shopVipUserPo.setUserId("-1");
            shopVipUserPo.setShopScope("0");
            shopVipUserPo.setVipId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_eventId));
            shopVipUserPo.setStoreId(storeId);
            shopVipUserPo.setBirthday(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
            shopVipUserPo.setVipName(licensePlate);
            shopVipUserPo.setSex(ShopVipUserDto.SEX_MAN);
            shopVipUserPo.setVipTel(tel);
            shopVipUserPo.setStartTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
            shopVipUserPo.setEndTime(DateUtil.getLastTime());
            shopVipUserPo.setVipLevel(shopVipLevelDtos.get(0).getVipLevelId());
            int detaFlag = shopVipUserInnerServiceSMOImpl.saveShopVipUser(shopVipUserPo);
            if (detaFlag < 1) {
                throw new IllegalArgumentException("默认添加会员信息失败");
            }

            //新建会员积分账号
            ShopVipAccountPo shopVipAccountPo = new ShopVipAccountPo();
            shopVipAccountPo.setAcctName(shopVipUserPo.getVipName());
            shopVipAccountPo.setAcctType("2004");
            shopVipAccountPo.setAmount("0");
            shopVipAccountPo.setShopId(shopVipUserPo.getShopId());
            shopVipAccountPo.setStoreId(shopVipUserPo.getStoreId());
            shopVipAccountPo.setVipAcctId(GenerateCodeFactory.getGeneratorId("11"));
            shopVipAccountPo.setVipId(shopVipUserPo.getVipId());
            detaFlag = shopVipAccountInnerServiceSMOImpl.saveShopVipAccount(shopVipAccountPo);
            if (detaFlag < 1) {
                return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "新建会员积分账号失败");
            }

            shopVipUserDtos = new ArrayList<>();
            ShopVipUserDto shopVipUser = new ShopVipUserDto();
            shopVipUser.setVipId(shopVipUserPo.getVipId());
            shopVipUser.setVipName(shopVipUserPo.getVipName());
            shopVipUser.setVipTel(shopVipUserPo.getVipTel());
            shopVipUserDtos.add(shopVipUser);
        }
        int runNumber = 0;
        int tmpStock = 0;

        //查询店铺所拥有的购物券，验证库存与扣除购物券关系
        CouponShopPoolDto couponShopPoolDto = new CouponShopPoolDto();
        couponShopPoolDto.setShopId(shopId);
        couponShopPoolDto.setSpId(spId);
        List<CouponShopPoolDto> couponShopPoolDtos = couponShopPoolV1InnerServiceSMOImpl.queryCouponShopPools(couponShopPoolDto);
        BigDecimal stock = new BigDecimal(couponShopPoolDtos.get(0).getStock());
        //库存为0的情况下直接返回不做购物券处理
        if (0 == stock.intValue()) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "赠送失败，购物券库存不足");
        }
        //校验库存与所需扣除数量，相等或者库存不够，库存有多少送多少，
        if ((stock.subtract(giftsCostMoney).setScale(0, BigDecimal.ROUND_DOWN).doubleValue()) <= 0) {
            runNumber = stock.intValue();
        } else {
            runNumber = giftsCostMoney.intValue();
        }
        //根据数量 按照一个购物券为一条数据，为用户写入购物券明细
        for (int userIndex = 0; userIndex < runNumber; userIndex++) {
            CouponUserPo couponUserPo = new CouponUserPo();
            couponUserPo.setCouponId(GenerateCodeFactory.getGeneratorId("11"));
            couponUserPo.setCouponName(couponShopPoolDtos.get(0).getCouponName());
            couponUserPo.setActualPrice(couponShopPoolDtos.get(0).getActualPrice());
            couponUserPo.setPoolId(couponShopPoolDtos.get(0).getPoolId());
            couponUserPo.setValidityDay(couponShopPoolDtos.get(0).getValidityDay());
            couponUserPo.setUserId(shopVipUserDtos.get(0).getVipId());
            couponUserPo.setUserName(shopVipUserDtos.get(0).getVipName());
            couponUserPo.setTel(shopVipUserDtos.get(0).getVipTel());
            couponUserPo.setState(CouponUserPo.STATE_RUN);
            int detaFlag = couponUserV1InnerServiceSMOImpl.saveCouponUser(couponUserPo);
            if (detaFlag < 1) {
                throw new IllegalArgumentException("购物券写入用户信息表失败");
            }
        }
        if ((stock.subtract(giftsCostMoney).setScale(0, BigDecimal.ROUND_DOWN).doubleValue()) <= 0) {
            tmpStock = 0;
        } else {
            tmpStock = stock.subtract(giftsCostMoney).setScale(0, BigDecimal.ROUND_DOWN).intValue();
        }
        //根据赠送数量刷新店铺购物券库存数据
        CouponShopPoolPo couponShopPoolPo = new CouponShopPoolPo();
        couponShopPoolPo.setSpId(couponShopPoolDtos.get(0).getSpId());
        couponShopPoolPo.setStock(tmpStock + "");
        int detaFlag = couponShopPoolV1InnerServiceSMOImpl.updateCouponShopPool(couponShopPoolPo);
        if (detaFlag < 1) {
            throw new IllegalArgumentException("店铺购物券刷新库存失败");
        }
        //记录赠送明细表
        CouponShopPoolDetailPo couponShopPoolDetailPo = new CouponShopPoolDetailPo();
        couponShopPoolDetailPo.setDetailId(GenerateCodeFactory.getGeneratorId("11"));
        couponShopPoolDetailPo.setCouponName(couponShopPoolDtos.get(0).getCouponName());
        couponShopPoolDetailPo.setPoolId(couponShopPoolDtos.get(0).getPoolId());
        couponShopPoolDetailPo.setShopId(couponShopPoolDtos.get(0).getShopId());
        couponShopPoolDetailPo.setSpId(couponShopPoolDtos.get(0).getSpId());
        couponShopPoolDetailPo.setActualPrice(couponShopPoolDtos.get(0).getActualPrice());
        couponShopPoolDetailPo.setSendCount(runNumber + "");
        couponShopPoolDetailPo.setValidityDay(couponShopPoolDtos.get(0).getValidityDay());
        couponShopPoolDetailPo.setOrderId("-1");
        couponShopPoolDetailPo.setUserId(shopVipUserDtos.get(0).getVipId());
        couponShopPoolDetailPo.setUserName(shopVipUserDtos.get(0).getVipName());
        couponShopPoolDetailPo.setTel(shopVipUserDtos.get(0).getVipTel());
        detaFlag = couponShopPoolDetailV1InnerServiceSMOImpl.saveCouponShopPoolDetail(couponShopPoolDetailPo);
        if (detaFlag < 1) {
            throw new IllegalArgumentException("记录店铺购物券赠送明细失败");
        }
        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "赠送成功！");
    }

    private void saveOrderAddress(StoreOrderPo storeOrderPo, String addressId) {

        UserAddressDto userAddressDto = new UserAddressDto();
        userAddressDto.setAddressId(addressId);
        userAddressDto.setUserId(storeOrderPo.getPersonId());
        List<UserAddressDto> userAddressDtos = userAddressInnerServiceSMOImpl.queryUserAddresss(userAddressDto);

        Assert.listOnlyOne(userAddressDtos, "未找到收货人信息");

        userAddressDto = userAddressDtos.get(0);

        StoreOrderAddressPo storeOrderAddressPo = new StoreOrderAddressPo();
        storeOrderAddressPo.setAddress(userAddressDto.getAddress());
        storeOrderAddressPo.setAddressId(userAddressDto.getAddressId());
        storeOrderAddressPo.setAreaCode(userAddressDto.getAreaCode());
        storeOrderAddressPo.setOaId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_oaId));
        storeOrderAddressPo.setOrderId(storeOrderPo.getOrderId());
        storeOrderAddressPo.setTel(userAddressDto.getTel());
        storeOrderAddressPo.setUsername(userAddressDto.getUsername());

        int flag = storeOrderAddressInnerServiceSMOImpl.saveStoreOrderAddress(storeOrderAddressPo);

        if (flag < 1) {
            throw new IllegalArgumentException("保存收货人信息失败");
        }

    }

    /**
     * 保存购物车
     *
     * @param goods        商品
     * @param storeOrderPo
     */
    private void saveStoreOrderCart(JSONObject goods, StoreOrderPo storeOrderPo, JSONObject param) {

        //Assert.hasKeyAndValue(goods, "cartId", "未包含购物车ID");
        Assert.hasKeyAndValue(goods, "productId", "未包含商品");
        Assert.hasKeyAndValue(goods, "prodName", "未包含商品");
        Assert.hasKeyAndValue(goods, "valueId", "未包含商品规格");
        Assert.hasKeyAndValue(goods, "goodsNum", "未包含商品数量");
        Assert.hasKeyAndValue(goods, "storeId", "未包含商户");
        Assert.hasKeyAndValue(goods, "shopId", "未包含商户");

        if (!goods.containsKey("cartId") || goods.getString("cartId").startsWith("-")) {
            goods.put("cartId", GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_cartId));
        }

        int flag = 0;
        ProductSpecValueDto productSpecValueDto = null;
        GroupBuyProductSpecDto groupBuyProductSpecDto = null;
        //开始锁代码
        String requestId = DistributedLock.getLockUUID();
        String key = goods.getString("productId") + goods.getString("valueId");
        try {
            DistributedLock.waitGetDistributedLock(key, requestId);
            productSpecValueDto = getProductSpecValueDto(goods);
            //查询是否 拼团产品
            groupBuyProductSpecDto = getGroupBuyProduct(productSpecValueDto);

        } finally {
            DistributedLock.releaseDistributedLock(requestId, key);
        }

        String payPrice = groupBuyProductSpecDto == null ? productSpecValueDto.getPrice() : groupBuyProductSpecDto.getGroupPrice();
        BigDecimal payPriceDeci = new BigDecimal(Double.parseDouble(payPrice));
        payPriceDeci = payPriceDeci.multiply(new BigDecimal(goods.getDouble("goodsNum"))).setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal tmpPayPriceDeci = payPriceDeci;
        StoreOrderCartPo storeOrderCartPo = new StoreOrderCartPo();
        storeOrderCartPo.setCartId(goods.getString("cartId"));
        storeOrderCartPo.setCartNum(goods.getString("goodsNum"));
        storeOrderCartPo.setFreightPrice("0");
        storeOrderCartPo.setOrderId(storeOrderPo.getOrderId());
        //考虑优惠
        if (goods.containsKey("discountPrice") && !StringUtil.isEmpty(goods.getString("discountPrice"))) {
            double discountPrice = goods.getDouble("discountPrice");
            payPriceDeci = payPriceDeci.subtract(new BigDecimal(discountPrice)).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        storeOrderCartPo.setPayPrice(payPriceDeci.doubleValue() + "");
        storeOrderCartPo.setPersonId(storeOrderPo.getPersonId());
        storeOrderCartPo.setProductId(goods.getString("productId"));
        storeOrderCartPo.setProdName(goods.getString("prodName"));
        storeOrderCartPo.setState(StoreOrderCartDto.STATE_WAIT_SEND);
        if (goods.containsKey("cartType")) {
            storeOrderCartPo.setCartType(StoreOrderCartDto.CART_TYPE_PRODUCT);
        } else {
            storeOrderCartPo.setCartType(goods.getString("cartType"));
        }
        if (AppDto.MALL_APP_ID.equals(storeOrderPo.getAppId())) {
            storeOrderCartPo.setState(StoreOrderCartDto.STATE_FINISH);
        }
        storeOrderCartPo.setStoreId(goods.getString("storeId"));
        storeOrderCartPo.setShopId(goods.getString("shopId"));
        storeOrderCartPo.setValueId(goods.getString("valueId"));
        storeOrderCartPo.setPrice(productSpecValueDto.getPrice());
        storeOrderCartPo.setAreaCode("");
        flag = storeOrderCartInnerServiceSMOImpl.saveStoreOrderCart(storeOrderCartPo);

        if (flag < 1) {
            throw new IllegalArgumentException("保存购物车失败");
        }

        //如果商品又优惠写入优惠
        if (goods.containsKey("discountPrice") && !StringUtil.isEmpty(goods.getString("discountPrice"))) {
            dealGoodsDiscountPrice(goods, storeOrderPo);
        }

        //积分商品扣积分
        if (isIntegralProduct(goods.getString("shopId"), goods.getString("productId"))) {
            withholdAccount(storeOrderCartPo, storeOrderPo, param);
        }

        StoreOrderCartEventPo storeOrderCartEventPo = new StoreOrderCartEventPo();
        storeOrderCartEventPo.setCartId(storeOrderCartPo.getCartId());
        storeOrderCartEventPo.setOrderId(storeOrderCartPo.getOrderId());
        storeOrderCartEventPo.setEventId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_eventId));
        storeOrderCartEventPo.setEventObjType("U");
        storeOrderCartEventPo.setEventObjId(storeOrderPo.getPersonId());
        storeOrderCartEventPo.setShopId(goods.getString("shopId"));
        storeOrderCartEventPo.setEventType(StoreOrderCartEventDto.EVENT_TYPE_COMMON);
        storeOrderCartEventPo.setEventMsg("用户下单");

        flag = storeOrderCartEventInnerServiceSMOImpl.saveStoreOrderCartEvent(storeOrderCartEventPo);

        if (flag < 1) {
            throw new IllegalArgumentException("保存购物车事件失败");
        }


        //师延俊加入 订单费用逻辑
        saveStoreOrderCartFee(storeOrderCartPo, storeOrderPo, goods);

        BigDecimal orderPayPrice = new BigDecimal(Double.parseDouble(storeOrderPo.getTotalPrice()));

        double oPayPrice = orderPayPrice.add(tmpPayPriceDeci).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        storeOrderPo.setTotalPrice(oPayPrice + "");

    }

    /**
     * 处理商品优惠
     *
     * @param goods
     * @param storeOrderPo
     */
    private void dealGoodsDiscountPrice(JSONObject goods, StoreOrderPo storeOrderPo) {
        double discountPrice = goods.getDouble("discountPrice");
        if (discountPrice == 0) { // 没有优惠
            return;
        }

        StoreOrderDiscountPo storeOrderDiscountPo = new StoreOrderDiscountPo();
        storeOrderDiscountPo.setDiscountId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_discountId));
        storeOrderDiscountPo.setDiscountObjId(goods.getString("cartId"));
        storeOrderDiscountPo.setDiscountObjType(StoreOrderDiscountDto.DISCOUNT_OBJ_TYPE_CART);
        storeOrderDiscountPo.setDiscountPrice(discountPrice + "");
        storeOrderDiscountPo.setDiscountRate(goods.getString("discountRate"));
        storeOrderDiscountPo.setDiscountType(goods.getString("discountType"));
        storeOrderDiscountPo.setShopId(goods.getString("shopId"));
        int flag = storeOrderDiscountInnerServiceSMOImpl.saveStoreOrderDiscount(storeOrderDiscountPo);
        if (flag < 1) {
            throw new IllegalArgumentException("记录优惠信息失败");
        }
    }

    private void withholdAccount(StoreOrderCartPo storeOrderCartPo, StoreOrderPo storeOrderPo, JSONObject param) {
        JSONObject vipuser = param.getJSONObject("vipUsers");
        if (null == vipuser || vipuser.size() < 1) {
            throw new IllegalArgumentException("未包含会员信息");
        }

        if (StringUtil.isNullOrNone(vipuser.getString("vipId"))) {
            throw new IllegalArgumentException("未包含会员信息");
        }


        ShopVipAccountDto shopVipAccountDto = new ShopVipAccountDto();
        shopVipAccountDto.setVipId(vipuser.getString("vipId"));
        shopVipAccountDto.setAcctType(ShopVipAccountDto.ACCT_TYPE_INTEGRAL);
        List<ShopVipAccountDto> shopVipAccountDtos = shopVipAccountInnerServiceSMOImpl.queryShopVipAccounts(shopVipAccountDto);
        if (shopVipAccountDtos == null || shopVipAccountDtos.size() < 1) {
            ShopVipAccountPo shopVipAccountPo = new ShopVipAccountPo();
            shopVipAccountPo.setVipAcctId(GenerateCodeFactory.getGeneratorId("11"));
            shopVipAccountPo.setAcctName("积分账户");
            shopVipAccountPo.setAmount("0");
            shopVipAccountPo.setShopId(storeOrderCartPo.getShopId());
            shopVipAccountPo.setStoreId(storeOrderCartPo.getStoreId());
            shopVipAccountPo.setVipId(vipuser.getString("vipId"));
            shopVipAccountPo.setAcctType("2004");
            int flag = shopVipAccountInnerServiceSMOImpl.saveShopVipAccount(shopVipAccountPo);
            if (flag < 1) {
                throw new IllegalArgumentException("新增积分账户失败");
            }
            ShopVipAccountDto vipAccountDto = new ShopVipAccountDto();
            vipAccountDto.setVipAcctId(shopVipAccountPo.getVipAcctId());
            vipAccountDto.setStoreId(shopVipAccountPo.getStoreId());
            vipAccountDto.setShopId(shopVipAccountPo.getShopId());
            vipAccountDto.setAmount(shopVipAccountPo.getAmount());
            vipAccountDto.setAcctType(shopVipAccountPo.getAcctType());
            shopVipAccountDtos.add(vipAccountDto);

        }

        //扣减积分
        ShopVipAccountDetailPo shopVipAccountDetailPo = new ShopVipAccountDetailPo();
        shopVipAccountDetailPo.setVipAcctId(shopVipAccountDtos.get(0).getVipAcctId());
        shopVipAccountDetailPo.setShopId(storeOrderCartPo.getShopId());
        shopVipAccountDetailPo.setOrderId(storeOrderPo.getOrderId());
        shopVipAccountDetailPo.setStoreId(storeOrderCartPo.getStoreId());
        shopVipAccountDetailPo.setAmount(storeOrderCartPo.getPayPrice());
        shopVipAccountDetailPo.setRemark("收银台累计积分");
        int flag = shopVipAccountInnerServiceSMOImpl.withholdAccount(shopVipAccountDetailPo);
        if (flag < 1) {
            throw new IllegalArgumentException("保存购物车失败");
        }

    }

    /**
     * 写订单费用
     *
     * @param storeOrderCartPo
     * @param storeOrderPo
     * @param goods
     */
    private void saveStoreOrderCartFee(StoreOrderCartPo storeOrderCartPo, StoreOrderPo storeOrderPo, JSONObject goods) {

        //判断是否为 家政服务
        StoreOrderCartFeePo storeOrderCartFeePo = new StoreOrderCartFeePo();
        storeOrderCartFeePo.setDetailId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_detailId));
        storeOrderCartFeePo.setCartId(storeOrderCartPo.getCartId());
        storeOrderCartFeePo.setShopId(storeOrderCartPo.getShopId());
        storeOrderCartFeePo.setFeeConfigId("-1");
        storeOrderCartFeePo.setFeeName(storeOrderCartPo.getProdName());
        storeOrderCartFeePo.setPrice(storeOrderCartPo.getPrice());
        storeOrderCartFeePo.setPayPrice(storeOrderCartPo.getPayPrice());
        storeOrderCartFeePo.setPayWay(goods.getString("payWay"));
        if (isIntegralProduct(storeOrderCartPo.getShopId(), storeOrderCartPo.getProductId())) {
            storeOrderCartFeePo.setPayWay(StoreOrderCartFeeDto.PAY_WAY_INTEGRAL);
        }
        //手机端支付
        if (AppDto.MALL_USER_APP_ID.equals(storeOrderPo.getAppId())) {
            storeOrderCartFeePo.setPayWay(StoreOrderCartFeeDto.PAY_WAY_WECHAT);
        }
        if (StringUtil.isEmpty(storeOrderCartFeePo.getPayWay())) {
            storeOrderCartFeePo.setPayWay(StoreOrderCartFeeDto.PAY_WAY_CASH);
        }

        //1001 未支付、2002 已支付
        storeOrderCartFeePo.setState("2002");
        storeOrderCartFeePo.setExtOrderId("-1");
        if (StringUtil.isEmpty(storeOrderCartPo.getRemark())
                || StringUtil.isEmpty(storeOrderPo.getRemark())) {
            storeOrderCartFeePo.setRemark("");
        } else {
            storeOrderCartFeePo.setRemark(storeOrderCartPo.getRemark() + '和' + storeOrderPo.getRemark());
        }
        storeOrderCartFeePo.setUpdateTime(DateUtil.getNow("yyyy-MM-dd HH:mm:ss"));
        int flag = storeOrderCartFeeInnerServiceSMOImpl.saveStoreOrderCartFee(storeOrderCartFeePo);
        if (flag < 1) {
            throw new IllegalArgumentException("订单费用失败");
        }

    }

    //判断是否为积分商品
    private boolean isIntegralProduct(String shopId, String productId) {

        ProductDto productDto = new ProductDto();
        productDto.setShopId(shopId);
        productDto.setProductId(productId);
        List<ProductDto> productDtos = productInnerServiceSMOImpl.queryProducts(productDto);

        if (productDtos == null || productDtos.size() < 1) {
            return false;
        }

        String cateType = productDtos.get(0).getCateType();
        if (ProductCategoryDto.CATE_TYPE_INTEGRAL_PUBLIC.equals(cateType)) {
            return true;
        }
        return false;

    }

    private ProductSpecValueDto getProductSpecValueDto(JSONObject goods) {
        ProductSpecValueDto productSpecValueDto;
        int flag;//查询 产品 及价格 并且 减库存
        productSpecValueDto = new ProductSpecValueDto();
        productSpecValueDto.setProductId(goods.getString("productId"));
        productSpecValueDto.setValueId(goods.getString("valueId"));
        productSpecValueDto.setShopId(goods.getString("shopId"));
        List<ProductSpecValueDto> productSpecValueDtos = productSpecValueInnerServiceSMOImpl.queryProductSpecValues(productSpecValueDto);
        Assert.listOnlyOne(productSpecValueDtos, "不存在 该产品信息");

        productSpecValueDto = productSpecValueDtos.get(0);

        int stock = Integer.parseInt(productSpecValueDto.getStock());

        if (stock < goods.getDouble("goodsNum")) {
            throw new IllegalArgumentException("库存不足");
        }
        ProductSpecValuePo productSpecValuePo = new ProductSpecValuePo();
        productSpecValuePo.setValueId(productSpecValueDto.getValueId());
        productSpecValuePo.setProductId(productSpecValueDto.getProductId());
        productSpecValuePo.setStoreId(productSpecValueDto.getStoreId());
        productSpecValuePo.setSales((Integer.parseInt(productSpecValueDto.getSales()) + goods.getDouble("goodsNum")) + "");
        productSpecValuePo.setStock((stock - goods.getDouble("goodsNum")) + "");
        flag = productSpecValueInnerServiceSMOImpl.updateProductSpecValue(productSpecValuePo);
        if (flag < 1) {
            throw new IllegalArgumentException("减库存失败");
        }
        return productSpecValueDto;
    }


    private GroupBuyProductSpecDto getGroupBuyProduct(ProductSpecValueDto productSpecValueDto) {
        GroupBuyProductSpecDto groupBuyProductSpecDto;
        int flag;
        groupBuyProductSpecDto = new GroupBuyProductSpecDto();
        groupBuyProductSpecDto.setStoreId(productSpecValueDto.getStoreId());
        groupBuyProductSpecDto.setProductId(productSpecValueDto.getProductId());
        groupBuyProductSpecDto.setValueId(productSpecValueDto.getValueId());
        List<GroupBuyProductSpecDto> groupBuyProductSpecDtos = groupBuyProductSpecInnerServiceSMOImpl.queryGroupBuyProductSpecs(groupBuyProductSpecDto);

        if (groupBuyProductSpecDtos == null || groupBuyProductSpecDtos.size() < 1) {
            return null;
        }

        groupBuyProductSpecDto = groupBuyProductSpecDtos.get(0);

        int groupStock = Integer.parseInt(groupBuyProductSpecDto.getStock());
        if (groupStock < 1) {
            return null;
        }
        GroupBuyProductSpecPo groupBuyProductSpecPo = new GroupBuyProductSpecPo();
        groupBuyProductSpecPo.setStoreId(productSpecValueDto.getStoreId());
        groupBuyProductSpecPo.setProductId(productSpecValueDto.getProductId());
        groupBuyProductSpecPo.setValueId(productSpecValueDto.getValueId());
        groupBuyProductSpecPo.setGroupStock((groupStock - 1) + "");
        groupBuyProductSpecPo.setGroupSales((Integer.parseInt(groupBuyProductSpecDto.getGroupSales()) + 1) + "");
        flag = groupBuyProductSpecInnerServiceSMOImpl.updateGroupBuyProductSpec(groupBuyProductSpecPo);
        if (flag < 1) {
            throw new IllegalArgumentException("减库存失败");
        }
        return groupBuyProductSpecDto;
    }

}